{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# cryptarithm_sat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/examples/cryptarithm_sat.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/python/cryptarithm_sat.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "Use CP-SAT to solve a simple cryptarithmetic problem: SEND+MORE=MONEY.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.sat.python import cp_model\n",
    "\n",
    "\n",
    "def send_more_money() -> None:\n",
    "    \"\"\"solve the cryptarithmic puzzle SEND+MORE=MONEY.\"\"\"\n",
    "    model = cp_model.CpModel()\n",
    "\n",
    "    # Create variables.\n",
    "    # Since s is a leading digit, it can't be 0.\n",
    "    s = model.new_int_var(1, 9, \"s\")\n",
    "    e = model.new_int_var(0, 9, \"e\")\n",
    "    n = model.new_int_var(0, 9, \"n\")\n",
    "    d = model.new_int_var(0, 9, \"d\")\n",
    "    # Since m is a leading digit, it can't be 0.\n",
    "    m = model.new_int_var(1, 9, \"m\")\n",
    "    o = model.new_int_var(0, 9, \"o\")\n",
    "    r = model.new_int_var(0, 9, \"r\")\n",
    "    y = model.new_int_var(0, 9, \"y\")\n",
    "\n",
    "    # Create carry variables. c0 is true if the first column of addends carries\n",
    "    # a 1, c2 is true if the second column carries a 1, and so on.\n",
    "    c0 = model.new_bool_var(\"c0\")\n",
    "    c1 = model.new_bool_var(\"c1\")\n",
    "    c2 = model.new_bool_var(\"c2\")\n",
    "    c3 = model.new_bool_var(\"c3\")\n",
    "\n",
    "    # Force all letters to take on different values.\n",
    "    model.add_all_different(s, e, n, d, m, o, r, y)\n",
    "\n",
    "    # Column 0:\n",
    "    model.add(c0 == m)\n",
    "\n",
    "    # Column 1:\n",
    "    model.add(c1 + s + m == o + 10 * c0)\n",
    "\n",
    "    # Column 2:\n",
    "    model.add(c2 + e + o == n + 10 * c1)\n",
    "\n",
    "    # Column 3:\n",
    "    model.add(c3 + n + r == e + 10 * c2)\n",
    "\n",
    "    # Column 4:\n",
    "    model.add(d + e == y + 10 * c3)\n",
    "\n",
    "    # solve model.\n",
    "    solver = cp_model.CpSolver()\n",
    "    if solver.solve(model) == cp_model.OPTIMAL:\n",
    "        print(\"Optimal solution found!\")\n",
    "    print(\"s:\", solver.value(s))\n",
    "    print(\"e:\", solver.value(e))\n",
    "    print(\"n:\", solver.value(n))\n",
    "    print(\"d:\", solver.value(d))\n",
    "    print(\"m:\", solver.value(m))\n",
    "    print(\"o:\", solver.value(o))\n",
    "    print(\"r:\", solver.value(r))\n",
    "    print(\"y:\", solver.value(y))\n",
    "\n",
    "\n",
    "def main(_) -> None:\n",
    "    send_more_money()\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
